package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "web渲染之list检索列表控制器")
@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    @ApiOperation(value = "检索列表")
    @RequestMapping("list.html")
    public String search(SearchParam searchParam, Model model){
        //Result<SearchResponseVo> ---Result,getDate()转为 Result<Map> 接收
        Result<Map> listResult = listFeignClient.list(searchParam);
        model.addAllAttributes(listResult.getData());
        model.addAttribute("searchParam",searchParam);

        //封装urlParam到页面，用于下次跳转时前面参数的记录/及页面其它用处
        String urlParam = makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);

        //检索页面的面包屑
        //品牌面包屑  trademarkParam
        String trademarkParam = makeTrademarkParam(searchParam.getTrademark());
        model.addAttribute("trademarkParam",trademarkParam);

        //平台属性面包屑
        List<Map> propsParamList = makePropsList(searchParam.getProps());
        model.addAttribute("propsParamList",propsParamList);

        //排序参数orderMap
        Map<String,Object> orderMap = dealOrder(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);

        return "list/index";
    }

    ////封装urlParam到页面，用于下次跳转时前面参数的记录
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder("list.html?");
        //追加三級分類形參
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            sb.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            sb.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            sb.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //追加关键词参数
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            sb.append("keyword=").append(searchParam.getKeyword());
        }
        //品牌参数
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            if (sb.length()>0){
                sb.append("&trademark=").append(trademark);
            }
        }
        //平台属性参数
        String[] props = searchParam.getProps();
        if (props !=null && props.length >0){
            if (sb.length()>0) {
                for (String prop : props) {
                    sb.append("&props=").append(prop);
                }
            }
        }
        return sb.toString();
    }

    //品牌面包屑  trademarkParam
    private String makeTrademarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            if (split != null && split.length == 2){
                return "品牌:"+split[1];
            }
        }
        return "";
    }

    //平台属性面包屑
    private List<Map> makePropsList(String[] props) {
        List<Map> list = new ArrayList<>();
        if (props !=null && props.length>0){
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    HashMap<Object, Object> map = new HashMap<>();
                    map.put("attrId",split[0]);
                    map.put("attrName",split[2]);
                    map.put("attrValue",split[1]);
                    list.add(map);
                }
            }
        }
        return list;
    }

    //排序参数orderMap   orderMap.type orderMap.sort
    private Map<String, Object> dealOrder(String order) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split != null && split.length == 2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        }else {
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }



}
